package com.mjfeed.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.mjfeed.Constants;
import com.mjfeed.entity.ForumTopic;
import com.mjfeed.entity.ProductCategory;
import com.mjfeed.service.ForumTopicService;
import com.mjfeed.dao.ForumTopicDao;
import com.mjfeed.util.DateUtil;
import com.mjfeed.util.Pagination;
import com.mjfeed.util.ParamBean;
import com.mjfeed.util.QueryCondition;

@Service("forumTopicService")
public class ForumTopicServiceImpl extends BaseServiceImpl implements ForumTopicService {
	@Resource(name="forumTopicDao")
    private ForumTopicDao forumTopicDao;
    
    @Transactional(readOnly = true)
	public Pagination<ForumTopic> getPagination(ParamBean paramBean) {
		List<QueryCondition> queryConditions = new ArrayList<QueryCondition>();
		if (StringUtils.isNotBlank(paramBean.getKeyword())) {
			String field = (paramBean.getField() == null || paramBean.getField().equals("")) ? "title" : paramBean.getField();
			queryConditions.add(new QueryCondition(field, QueryCondition.LK, paramBean.getKeyword()));
		}
		if (paramBean.getProductCategoryId() != null && paramBean.getProductCategoryId().longValue() > 0) {
			ProductCategory productCategory = getById(ProductCategory.class, paramBean.getProductCategoryId());
			if (productCategory != null) {
				queryConditions.add(new QueryCondition("productCategory", QueryCondition.EQ, productCategory));
			}
		}
		if (paramBean.getForumType() > -1) {
			queryConditions.add(new QueryCondition("type", QueryCondition.EQ, paramBean.getForumType()));
		} else {
			queryConditions.add(new QueryCondition("type", QueryCondition.EQ, ForumTopic.FORUM_TOPIC_TYPE));
		}
		String orderBy = "order by id desc";
		Pagination<ForumTopic> pagination = (Pagination<ForumTopic>)getPagination(ForumTopic.class, queryConditions, orderBy, paramBean.getPage(), paramBean.getPageSize());
		pagination.setPageHtml(paramBean.getUrl());
		return pagination;
	}

    @Transactional(readOnly = true)
	public long getTodayCount() {
    	List<ForumTopic> forumTopics = getByJpql("from ForumTopic where type=0 and DATE_FORMAT(createDate,'%Y-%m-%d') like ?", DateUtil.format(new Date(), DateUtil.defaultDatePattern));
    	if (forumTopics != null && forumTopics.size() > 0) {
			return forumTopics.size();
		}
		return 0;
	}

    @Transactional(readOnly = true)
	public long getYesterdayCount() {
    	List<ForumTopic> forumTopics = getByJpql("from ForumTopic where type=0 and DATE_FORMAT(createDate,'%Y-%m-%d') like ?", DateUtil.format(DateUtil.getYesterdayCurrentDate(), DateUtil.defaultDatePattern));
    	if (forumTopics != null && forumTopics.size() > 0) {
			return forumTopics.size();
		}
		return 0;
	}

    @Transactional(readOnly = true)
	public long getCount() {
    	List<QueryCondition> queryConditions = new ArrayList<QueryCondition>();
    	queryConditions.add(new QueryCondition("type", QueryCondition.EQ, ForumTopic.FORUM_TOPIC_TYPE));
		return getRecordCount(ForumTopic.class, queryConditions);
	}

	public void build(ForumTopic forumTopic) {
		MultipartFile multipartFile = forumTopic.getFile();
		if (multipartFile != null && !multipartFile.isEmpty()) {
			try {
				String uploadPath = Constants.UPLOAD_PATH_PREFIX_COLLEGETOPIC + DateFormatUtils.format(new Date(), "yyyyMMdd")+"/";
				String uuid = UUID.randomUUID().toString();
				String sourcePath = uploadPath + uuid + FilenameUtils.EXTENSION_SEPARATOR + FilenameUtils.getExtension(multipartFile.getOriginalFilename());

				File sourceFile = new File(sourcePath);
				if (!sourceFile.getParentFile().exists()) {
					sourceFile.getParentFile().mkdirs();
				}
				multipartFile.transferTo(sourceFile);
				forumTopic.setSource(sourcePath);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
